<?php
namespace Mentees\Controller;

class RequestController extends MainController {

    public function index(){
 
        $status = I('get.status');
        if($status!=""){
           $this->assign('status', $status);
           if($status!=-1){
              $map['status']=$status;
           }
        }else{
          $this->assign('status', -1);  
        }
        $map['status']=array("neq",2);
        $obj = M('Apprentice');
        $map['toID']=$this->userID;
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 10);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    //请求详情
    public function view(){
        $id = (int)I('get.id');
        $obj = M('Apprentice');
        $map['toID']=$this->userID;
        $map['id']=$id;
        $list = $obj->where($map)->find();
        if (!$list) {
            $this->error(L('_public_noinfo'));
        } else {
            unset($map);
            $map['userid']=$list['fromID'];
            $mentor=M("Mentors")->where($map)->find();
            $mentor['trainItem'] = trainStrtoArr($mentor['trainItem']);
            $where['id'] = array('in',$mentor['trainItem']);
            $mentor['trainItem'] = M('TrainCate')->field('name')->where($where)->select();
            $this->assign('mentor',$mentor);
            $this->assign('list',$list);
            $this->display();
        }
    }

    public function save(){
        $obj = M('Apprentice');
        $nid=I('post.id');
        $map['toID'] = $this->userID;
        $map['id'] = $nid;      
        $info = $obj->where($map)->find();  
        if (!$info) {
            $state = L('_public_noinfo');
            echo $this->echo_json_str($state); die;
        }  

        $data['status'] = I('post.status');
        $data['remark'] = I('post.remark');
        $data['updateTime'] = time(); 
        if ($data['status']=='') {
            $state = 'Please choose the status';
            echo $this->echo_json_str($state); die;
        }

        if ($data['status']==2 && $data['remark']=='') {
            $state = 'Please enter a reason to refuse';
            echo $this->echo_json_str($state); die;
        }
               
        $list = $obj->where($map)->save($data);
        if ($list) {
            //添加到培训详情表
            unset($data);
            unset($map);
            $data['menteesID'] = $info['toID'];
            $data['mentorsID'] = $info['fromID'];
            $data['trainItem'] = $info['trainItem'];
            $data['skills'] = $info['skills'];
            $data['train'] = $info['train'];
            $data['target'] = $info['target'];
            $data['work'] = $info['work'];
            $data['createTime'] = time();
            $data['work'] = $info['work'];

            $map['menteesID'] = $info['toID'];
            $map['mentorsID'] = $info['fromID'];
            $map['trainItem'] = $info['trainItem'];
            $list = M('Train')->where($map)->find();

            if (!$list) {
                M('Train')->add($data);
            }

            unset($map);
            $map['toID'] = $this->userID;
            $map['id'] = $nid;
            M('Apprentice')->where($map)->delete();     

            $state = 'SUCCESS';
            $message = L('_action_success');
            $url = U('Request/index');
        }else{
            $state = L('_action_fail');
        }        
        echo $this->echo_json_str($state,$message,$url);
    }
}
